package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.conmmon.R;
import com.itheima.domain.Category;
import com.itheima.domain.Employee;
import com.itheima.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author liwanhu
 * @datetime 2021/10/25 15:41
 */

/**
 * 分类管理
 */
@RestController
@RequestMapping("category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加菜品成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page> page(int page ,int pageSize){
        log.info("分页{}",page);
        //创建分页对象,将当前页,和每页显示数放到参数中
        Page<Category> pageInfo = new Page<>(page,pageSize);
        //构造条件
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        //排序按序列
        queryWrapper.orderByAsc("sort");
        //调用IService中的page()分页方法
        categoryService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){
        log.info("删除分类");
        categoryService.remove(id);
        return R.success("分类信息删除成功");
    }

    /**
     * 根据id修改菜品信息
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("菜品信息的修改");
        categoryService.updateById(category);
        return R.success("信息修改成功");
    }

   /* *//**
     * 菜品分类列表(下拉框)
     * @param category
     * @return
     */
    @GetMapping("list")
    public R<List<Category>> list(Category category){
        //条件构造器
        QueryWrapper<Category> qw = new QueryWrapper<>();
        //查询分类的属性值1位菜品分类2为套餐
        qw.eq(category.getType() !=null,"type",category.getType());
        qw.orderByAsc("sort").orderByDesc("update_time");
        List<Category> list = categoryService.list(qw);
        return R.success(list);
    }
}
